﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Data;
using System.Windows.Media;
using System.Globalization;

namespace KegManager
{
    [ValueConversion(typeof(double), typeof(double))]
    public sealed class FormattingConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (parameter != null)
            {
                string formatstring = parameter.ToString();
                if (!String.IsNullOrEmpty(formatstring))
                {
                    return String.Format(culture, formatstring, value);
                }
            }

            return value.ToString();
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    [ValueConversion(typeof(double), typeof(double))]
    public sealed class TemperatureRangeConverter : IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        {
            SolidColorBrush color = Brushes.Green;
            if (values.Length == 3)
            {
                double? high = null;
                if (values[0] != null)
                {
                    high = System.Convert.ToDouble(values[0]);
                }
                
                double? low = null;
                if (values[1] != null)
                {
                    low = System.Convert.ToDouble(values[1]);
                }
                
                double val = System.Convert.ToDouble(values[2]);

                if ((low != null && val < low) || (high != null && val > high))
                {
                    color = Brushes.Red;
                }
            }

            return color;
        }

        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    [ValueConversion(typeof(double), typeof(double))]
    public sealed class TemperatureConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            // Convert the celcius value to the unit we want
            string system = Properties.Settings.Default.TemperatureUnit;
            double dValue = double.Parse(value.ToString());
            if (String.Compare(system, "fahrenheit", true) == 0)
            {
                dValue =  (1.8 * System.Convert.ToDouble(value)) + 32.0;
            }
            else if (String.Compare(system, "kelvin", true) == 0)
            {
                dValue = 273.15 + System.Convert.ToDouble(value);
            }

            return dValue.ToString(parameter as string) + "°";
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            // Convert the value back to celcius
            string system = Properties.Settings.Default.TemperatureUnit;
            double dValue = double.Parse(value.ToString());
            if (String.Compare(system, "fahrenheit", true) == 0)
            {
                dValue = (5 / 9) * (System.Convert.ToDouble(value) - 32.0);
            }
            else if (String.Compare(system, "kelvin", true) == 0)
            {
                dValue = System.Convert.ToDouble(value) - 273.15;
            }

            return dValue;
        }
    }

    [ValueConversion(typeof(double), typeof(double))]
    public sealed class SmallVolumeConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            // Convert liters to ounces
            string system = Properties.Settings.Default.VolumeUnit;
            double dValue = double.Parse(value.ToString());
            if (String.Compare(system, "gallon", true) == 0)
            {
                dValue = double.Parse(value.ToString()) * 33.814023;
            }
            return dValue.ToString(parameter as string);
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            string system = Properties.Settings.Default.VolumeUnit;
            double dValue = 0.0;
            if (String.Compare(system, "gallon", true) == 0)
            {
                dValue = System.Convert.ToDouble(value) / 33.814023;
            }
            return dValue;
        }
    }

    public sealed class SmallVolumeUnitConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            // Convert liters to ounces
            string system = Properties.Settings.Default.VolumeUnit;
            if (String.Compare(system, "gallon", true) == 0)
            {
                if (parameter.ToString() == "short")
                {
                    return "oz";
                }
                else
                {
                    return "ounces";
                }
            }
            return "L";
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    [ValueConversion(typeof(double), typeof(double))]
    public sealed class LargeVolumeConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            // Convert liters to gallons
            string system = Properties.Settings.Default.VolumeUnit;
            double dValue = double.Parse(value.ToString());
            if (String.Compare(system, "gallon", true) == 0)
            {
                dValue = System.Convert.ToDouble(value) * 0.26417205;
            }
            return dValue.ToString(parameter as string);
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            string system = Properties.Settings.Default.VolumeUnit;
            double dValue = double.Parse(value.ToString());
            if (String.Compare(system, "gallon", true) == 0)
            {
                dValue = System.Convert.ToDouble(value) / 0.26417205;
            }
            return dValue;
        }
    }

    //[ValueConversion(typeof(string), typeof(double))]
    //public sealed class TemperatureConverter : IValueConverter
    //{
    //    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    //    {
    //        // Round to only a single decimal place.  Any more than that is rather useless for display.
    //        string s = System.Math.Round((double)value, 1).ToString();

    //        // Add a degree symbol
    //        s += "°";

    //        return s;
    //    }

    //    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    //    {
    //        throw new NotImplementedException();
    //    }
    //}
}
